package com.apes.framework.plugin.form.parser.matcher;

import com.apes.framework.plugin.log.timer.Timer;
import com.apes.framework.util.Operation;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;
import java.util.regex.Pattern;

/**
 * 功能：逻辑操作模式匹配器
 *
 * @author xul
 * @create 2018-04-19 16:20
 */
public class OperationMatcher implements Matcher<String, List<String>> {

    private Matcher parent;

    /**
     * 功能：获取识别模式
     */
    @Override
    public String getRegex() {
        return ",";
    }

    @Override
    public Matcher getParent() {
        return parent;
    }

    @Override
    public void setParent(Matcher parent) {
        this.parent = parent;
    }

    /**
     * 功能：模式配置
     *
     * @param content
     */
    @Override
//    @Timer("解析逻辑操作表达式")
    public List<String> parse(String content) {
        List<String> result = new ArrayList();
        String[] arr = content.split(getRegex());
        for (int i = 0; i < arr.length; i++) {
            String text = getText(arr[i].trim());
            if (text == null) continue;
            if (Operation.isLogicOperation(text)) {
                result.add(text);
            }
        }
        return result;
    }

    private String getText(String content) {
        Pattern pattern = Pattern.compile("(')(.+?)(')");
        java.util.regex.Matcher matcher = pattern.matcher(content);
        while (matcher.find()) {
            return matcher.group(2).trim();
        }
        return null;
    }
}
